/**
 * 
 */
package com.huiquan.framework.utils;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * @author mingxing.fmx
 *
 */
public class JsonUtil {

    private static Logger log = LogManager.getLogger(JsonUtil.class.getName());

    /** 将对象转化为 JSON字符串 */
    public static String toJsonStr(Object obj) {
        ObjectMapper objectMapper = new ObjectMapper();

        String value = "";

        try {
            value = objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            log.error(e);
        }

        return value;
    }
    
    public static Object jsonToBean(String json, Class<?> cls) {
        ObjectMapper objectMapper = new ObjectMapper();
        Object vo = null;
        try {
            vo = objectMapper.readValue(json, cls);
        } catch (Exception e) {
            log.error("jsonToBean catched Exception!");
            log.error(e.getMessage(), e);
            return null;
        }   
        return vo;   
    }  
}
